package xmiparse;

import java.io.File;
import java.io.IOException;
import java.util.Stack;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import net.sf.jColtrane.handler.JColtraneXMLHandler;

import org.xml.sax.InputSource;
import org.xml.sax.SAXException;


import xmibase.XmiPackage;
import xmijcoltrane.XmiBuilder;
import xmijcoltrane.XmiGetAbstraction;
import xmijcoltrane.XmiGetAssociation;
import xmijcoltrane.XmiGetAttribute;
import xmijcoltrane.XmiGetGeneralization;
import xmijcoltrane.XmiGetTagDefinition;


public class XmiParser {

	private File file;
	public Stack elements;
	public XmiPackage rootPackage;

	/**
	 * Constructor
	 *
	 * @param classes
	 *            - array to build the classes from xmi file
	 * @param xmiPath
	 *            -
	 */
	public XmiParser(XmiPackage rootPackage, String xmiPath) {

		this.rootPackage = rootPackage;
		file = new File(xmiPath);

	}

	/**
	 * Execute the parse, building the array with all classes
	 */
	public void execute() {
		SAXParser parser = null;

		try {
			parser = SAXParserFactory.newInstance().newSAXParser();
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		}

		if (parser != null) {
			InputSource input = new InputSource(file.getAbsolutePath());
			try {
				System.out.println("Start Parse \n-------");
				elements = new Stack();
				parser.parse(input, new JColtraneXMLHandler(
						new XmiBuilder(rootPackage, elements)
						,new XmiGetAttribute(rootPackage, elements)
						,new XmiGetAssociation(rootPackage, elements)
						,new XmiGetGeneralization(rootPackage, elements)
						,new XmiGetTagDefinition(rootPackage, elements)
						,new XmiGetAbstraction(rootPackage, elements)


				));

				System.out.println("End Parse \n-------");
			} catch (SAXException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}

		}

	}

}